package com.taotao.sso.web;

import com.taotao.common.ExceptionUtil;
import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.TbUser;
import com.taotao.sso.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册 信息检查
     * @param param
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/check/{param}/{type}")
    public TaotaoResult checkData(@PathVariable String param,@PathVariable Integer type){
        TaotaoResult taotaoResult = userService.checkData(param,type);
        return taotaoResult;
    }

    /**
     * 用户注册
     * @param tbUser
     * @return
     */
    @ResponseBody
    @PostMapping("/register")
    public TaotaoResult registerUser(TbUser tbUser){
        TaotaoResult taotaoResult = userService.register(tbUser);
        return taotaoResult;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @PostMapping("/login")
    public TaotaoResult loginUser(String username, String password, HttpServletRequest request, HttpServletResponse response){
        TaotaoResult taotaoResult = userService.login(username,password,request,response);
        return taotaoResult;
    }

    /**
     * 按照Token获取user登录信息
     * @param token
     * @param callback
     * @return
     */
    @ResponseBody
    @CrossOrigin(origins = {"localhost","127.0.0.1","www.taotao.com"})
    @GetMapping("/token/{token}")
    public Object getUserByToken(@PathVariable String token, @RequestParam(required = false) String callback) {
        TaotaoResult taotaoResult = userService.getUserByToken(token);
        if (!StringUtils.isBlank(callback)) {
            return callback + "(" + JsonUtils.objectToJson(taotaoResult) + ");";
        }
        return taotaoResult;
    }

    @CrossOrigin(origins = {"localhost", "127.0.0.1", "www.taotao.com"})
    @RequestMapping("/logout/{token}")
    public Object logOutUser(@PathVariable String token, @RequestParam(required = false) String callback, HttpServletRequest request, HttpServletResponse response) {
        TaotaoResult taotaoResult = null;
        try {
            taotaoResult = userService.logOutUser(token, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            taotaoResult = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        if (StringUtils.isBlank(callback)) {
            return "login";
        } else {
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(taotaoResult);
            mappingJacksonValue.setValue(callback);
            return mappingJacksonValue;
        }
    }
}
